package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.StringUtil;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.utils.UtilPay;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**
 * 预约成功信息展示页-付款页面
 * 
 * @author Administrator
 */
public class GhSix extends Activity
{
	private ImageButton ks_preview = null;
	private CustomProgressDialog proDialog;// 加载框
	private GhSix oThis = this;
	private List<Map<String, Object>> list_yy = new ArrayList<Map<String, Object>>();;
	private Intent mIntent;
	private String strysgh;
	private String strpbdate;
	private String pbmxid;
	private String ghf;
	private String ksname;
	private String ksid;
	private String ysname;
	private String patid = "";
	private String cardno = "";
	private LinearLayout gh_six_ll;
	MyApplication myApp;
	private TextView hzxm, sfzh, sjh, mzsj, ghks, ysxm, ghfy, czrq, mzdd, yzm1, card;
	String yzm;
	private ImageView menu_img;
	private ImageButton mainselected;
	private Button zxff;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.gh_six);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		// 获取控件
		findView();

		// 获取数据
		getData();

		// 设置监听事件
		setListener();
	}

	private void findView()
	{
		mIntent = getIntent();
		if (mIntent != null)
		{
			strpbdate = mIntent.getStringExtra("strpbdate");
			strysgh = mIntent.getStringExtra("strysgh");
			ghf = mIntent.getStringExtra("ghf");
			pbmxid = mIntent.getStringExtra("pbmxid");
			ksname = mIntent.getStringExtra("ksname");
			ksid = mIntent.getStringExtra("ksid");
			ysname = mIntent.getStringExtra("ysname");
			patid = mIntent.getStringExtra("patid");
			cardno = mIntent.getStringExtra("cardno");
		}

		hzxm = (TextView) this.findViewById(R.id.hzxm);
		sfzh = (TextView) this.findViewById(R.id.sfzh);
		sjh = (TextView) this.findViewById(R.id.sjh);
		mzsj = (TextView) this.findViewById(R.id.mzsj);
		ghks = (TextView) this.findViewById(R.id.ghks);
		ysxm = (TextView) this.findViewById(R.id.ysxm);
		ghfy = (TextView) this.findViewById(R.id.ghfy);
		czrq = (TextView) this.findViewById(R.id.czsj);
		mzdd = (TextView) this.findViewById(R.id.mzdd);
		yzm1 = (TextView) this.findViewById(R.id.gh_six_yzm);
		card = (TextView) this.findViewById(R.id.card);
		// gh_six_ll =(LinearLayout)this.findViewById(R.id.gh_six_ll);
		menu_img = (ImageView) this.findViewById(R.id.menu_yy_img);
		mainselected = (ImageButton) this.findViewById(R.id.mainselected); // 返回主界面
		ks_preview = (ImageButton) this.findViewById(R.id.ghyy_preview); // 返回按钮
		zxff = (Button) this.findViewById(R.id.zxff);// 在线支付
		if (myApp.getGhpayflag() != null && myApp.getGhpayflag().equals("1"))
		{
			zxff.setVisibility(View.VISIBLE);
		}
		else
		{
			zxff.setVisibility(View.GONE);
		}
	}

	private void getData()
	{
		proDialog = CustomProgressDialog.createDialog(this);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();
	}

	private void setListener()
	{

		ks_preview.setOnClickListener(new View.OnClickListener()
		{// 返回按钮

					@Override
					public void onClick(View v)
					{
						// TODO Auto-generated method stub

						finish();

					}
				});

		// menu_img.setOnClickListener(new OnClickListener()
		// {//我的预约按钮
		//
		// @Override
		// public void onClick(View view)
		// {
		// // TODO Auto-generated method stub
		// Intent intent = new Intent();
		// intent.setClass(oThis, GhSeven.class);
		// startActivity(intent);
		// }
		// });

		mainselected.setOnClickListener(new OnClickListener()
		{// 返回主菜单

					@Override
					public void onClick(View view)
					{
						// TODO Auto-generated method stub
						Intent intent = new Intent();
						intent.setClass(oThis, Mainmean.class);
						startActivity(intent);
						oThis.finish();
					}
				});

		zxff.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				// 拿到Fiap对象并传入容器
				// Fiap fiap = new Fiap(GhSix.this);
				// // 调用支付方法，并传入支付金额
				// fiap.android_pay(0.01);
				try
				{
					double ghf = Double.parseDouble(ghfy.getText().toString());

					UtilPay pay = new UtilPay();

					if (ghf > 0)
					{
						pay.android_pay(URLUtils.HOST, list_yy.get(0).get("ghdh").toString(), ghf, GhSix.this, "挂号费用", "挂号费用");
					}
					else
					{
						Toast.makeText(oThis, "支付金额为负数，请检查！", Toast.LENGTH_SHORT).show();
					}

				}
				catch (Exception e)
				{
					Toast.makeText(oThis, "支付金额异常，请检查！", Toast.LENGTH_SHORT).show();

				}

			}
		});
	}

	class LoadData implements Runnable
	{

		public void run()
		{
			// TODO Auto-generated method stub
			Looper.prepare();
			iniYYxx();
			Looper.loop();
		}
	}

	private void iniYYxx()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("strpbdate", strpbdate));
			ls.add(new BasicNameValuePair("strysgh", strysgh));
			ls.add(new BasicNameValuePair("userid", myApp.getAccount()));

			ls.add(new BasicNameValuePair("ksname", ksname));
			ls.add(new BasicNameValuePair("ksid", ksid));
			ls.add(new BasicNameValuePair("ysname", ysname));

			ls.add(new BasicNameValuePair("pbmxid", pbmxid));
			ls.add(new BasicNameValuePair("ghf", ghf));
			ls.add(new BasicNameValuePair("sfzh", myApp.getSfzh()));
			ls.add(new BasicNameValuePair("sex", myApp.getSex()));
			ls.add(new BasicNameValuePair("hzxm", myApp.getHzxm()));
			ls.add(new BasicNameValuePair("sjh", myApp.getSjh()));
			ls.add(new BasicNameValuePair("memberid", myApp.getMemberid()));// 家庭成员id
			ls.add(new BasicNameValuePair("cardno", cardno));

			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.GHYYSAVE, ls);
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			if (json != null && !json.equals(""))
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					if (!json.getString("yyInfo").equals(""))
					{
						if (json.getString("yyInfo").indexOf("@") != -1)
						{
							msg.what = -1;
							msg.obj = json.getString("yyInfo").split("@")[0];
						}
						else
						{
							// 获取yy信息
							JSONArray jsonArr = new JSONArray(json.getString("yyInfo"));
							JSONObject js = null;
							for (int i = 0; i < jsonArr.length(); i++)
							{
								Map<String, Object> map_yy = new HashMap<String, Object>();
								js = jsonArr.getJSONObject(i);
								map_yy.put("ghdh", js.getString("ghdh"));
								map_yy.put("yymc", js.getString("yymc"));
								map_yy.put("ksmc", js.getString("ksmc"));
								map_yy.put("ysxm", js.getString("ysxm"));
								map_yy.put("yyrq", js.getString("yyrq"));
								map_yy.put("czrq", js.getString("czrq"));
								map_yy.put("sfzh", js.getString("sfzh"));
								map_yy.put("sjh", js.getString("sjh"));
								map_yy.put("zsxm", js.getString("zsxm"));
								map_yy.put("ghf", js.getString("ghf"));
								if (js.has("yzm"))
								{
									map_yy.put("yzm", js.getString("yzm"));
									yzm = js.getString("yzm");
								}
								else
								{
									yzm = "";
								}

								tempmain.add(map_yy);
							}
							msg.what = 0;
							msg.obj = tempmain;

						}

					}
					else
					{
						msg.what = -1;
						msg.obj = "预约失败";
					}

				}
				else
				{
					msg.what = -1;
					msg.obj = "系统错误";
				}

			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接失败!";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			// 捕捉异常
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					proDialog.dismiss();
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					oThis.finish();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					list_yy = (List<Map<String, Object>>) msg.obj;
					Toast.makeText(oThis, "预约成功", Toast.LENGTH_SHORT).show();
					setys();
					break;
				case 1:
					proDialog.dismiss();
					// setHosInfo();
					break;

				default:
					break;
			}
		};
	};

	private void setys()
	{
		hzxm.setText("患者姓名:" + list_yy.get(0).get("zsxm"));
		sfzh.setText("身份证号:" + list_yy.get(0).get("sfzh"));
		sjh.setText("手机号:" + list_yy.get(0).get("sjh"));
		mzsj.setText("门诊时间:" + list_yy.get(0).get("yyrq"));
		ghks.setText("挂号科室:" + list_yy.get(0).get("ksmc"));
		ysxm.setText("医生姓名:" + list_yy.get(0).get("ysxm"));
		ghfy.setText(list_yy.get(0).get("ghf") + "");
		czrq.setText("操作时间:" + list_yy.get(0).get("czrq"));
		if (StringUtil.isEquals("hb_qhd_01", myApp.getYydm()))
		{
			mzdd.setText("换号地点:有就诊卡卡号的患者请拿此卡号就诊卡到医院自助机或者相应科室护士分诊台取预约号序就诊。没有就诊卡的患者请到办卡结算中心办理就诊卡。");
		}
		else
		{
			mzdd.setText("换号地点:门诊预约服务中心");
		}
		if (yzm.equals(""))
		{
			yzm1.setVisibility(View.GONE);
		}
		else
		{
			yzm1.setVisibility(View.VISIBLE);
			yzm1.setText("验证码:" + list_yy.get(0).get("yzm"));
		}
		if (StringUtil.isEmpty(cardno))
		{
			card.setVisibility(View.GONE);
		}
		else
		{
			card.setVisibility(View.VISIBLE);
			card.setText("就诊卡号:" + cardno);
		}

	}

}
